﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Alta.Class
{
    public class mysql_helpper
    {
        private static string connectionstring = string.Empty;       
        public static void connection(string user, string host, string pass, string database)
        {
            connectionstring = @"server=" + host + ";uid=" + user + ";pwd=" + pass + ";database=" + database + ";"; ;
            
        }

        public static void setCommand(string content,string comname, string platform="C#")
        {
            if (string.IsNullOrEmpty(connectionstring))
                throw new Exception("Hãy kết nối cơ sở dữ liệu");
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionstring))
                {
                    conn.Open();
                    using (MySqlCommand command = new MySqlCommand())
                    {
                        command.Connection = conn;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "runCommand";
                        command.Parameters.Add(new MySqlParameter("@_command_recevice", MySqlDbType.VarChar, 100) { Direction = System.Data.ParameterDirection.Input, Value = content });
                        command.Parameters.Add(new MySqlParameter("@_command_platform", MySqlDbType.VarChar, 10) { Direction = System.Data.ParameterDirection.Input, Value = platform });
                        command.Parameters.Add(new MySqlParameter("@_com_name", MySqlDbType.VarChar, 10) { Direction = System.Data.ParameterDirection.Input, Value = comname });
                        command.ExecuteNonQuery();
                    }
                    conn.Close();
                }
            }
            catch (Exception)
            {

            }
        }

        public static bool setStatus(int id, bool status)
        {
            if (string.IsNullOrEmpty(connectionstring))
                throw new Exception("Hãy kết nối cơ sở dữ liệu");
            var flag = false;
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionstring))
                {
                    conn.Open();
                    using (MySqlCommand command = new MySqlCommand())
                    {
                        command.Connection = conn;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "setStatusCommand";
                        command.Parameters.Add(new MySqlParameter("@_command_id", MySqlDbType.Int32) { Direction = System.Data.ParameterDirection.Input, Value=id });
                        command.Parameters.Add(new MySqlParameter("@_command_status", MySqlDbType.Int32) { Direction = System.Data.ParameterDirection.Input, Value = status?1:0 });
                        command.Parameters.Add(new MySqlParameter("@_command_result", MySqlDbType.Int16) { Direction = System.Data.ParameterDirection.Output });
                        command.ExecuteNonQuery();
                        flag= Convert.ToBoolean(command.Parameters["@_command_result"].Value);
                    }
                    conn.Close();
                }
            }
            catch (Exception ex)
            {

            }
            return flag;
        }

        public static Command getCommand()
        {
            if (string.IsNullOrEmpty(connectionstring))
                throw new Exception("Hãy kết nối cơ sở dữ liệu");
            Command cmd = null;
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionstring))
                {
                    conn.Open();
                    using (MySqlCommand command = new MySqlCommand())
                    {
                        command.Connection = conn;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "getCommand";
                        command.Parameters.Add(new MySqlParameter("@_command_content", MySqlDbType.VarChar, 200) { Direction = System.Data.ParameterDirection.Output });
                        command.Parameters.Add(new MySqlParameter("@_command_com_name", MySqlDbType.VarChar, 10) { Direction = System.Data.ParameterDirection.Output });
                        command.Parameters.Add(new MySqlParameter("@_command_id", MySqlDbType.Int32) { Direction = System.Data.ParameterDirection.Output });
                        command.ExecuteNonQuery();
                        var content = command.Parameters["@_command_content"].Value.ToString();
                        var name = command.Parameters["@_command_com_name"].Value.ToString();
                        var id = Convert.ToInt32(command.Parameters["@_command_id"].Value);
                        cmd = new Command() { name = name, content = content, id=id };
                    }
                    conn.Close();
                }
            }
            catch (Exception ex)
            {

            }
            return cmd;
        }

    }
}
